package com.mmall.controller.backend;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServiceResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICategroyService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by xinxuchu on 2019/3/2.
 */
@Controller
@RequestMapping("/manage/category")
public class CategoryManagerController {


    @Autowired
    private IUserService iUserService;

    @Autowired
    private ICategroyService iCategroyService;

    /**
     * 新增节点
     *
     * @param session      确定当前用户是不是管理员
     * @param categroyname 新增节点name
     * @param parentid     新增节点的父节点，当前端未返回父节点的时候，默认父节点为根节点
     * @return
     */

    @RequestMapping(value = "add_category.do")
    @ResponseBody
    public ServiceResponse addCatagroy(HttpSession session, String categroyname, @RequestParam(value = "parentid", defaultValue = "0") int parentid) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请登录！");
        }
        //校验一下是否管理员
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //是管理员，增加处理分类的逻辑
            return iCategroyService.addCatagroy(categroyname,parentid);
        } else {
            return ServiceResponse.createByError("无权限在操作，需要管理员权限！");
        }
    }


    /**
     * 修改categroy的name
     * @param session  确定当前用户是不是管理员
     * @param categroyid  品类节点id
     * @param categroyname  品类节点name
     * @return
     */
    @RequestMapping("set_category_name.do")
    @ResponseBody
    public ServiceResponse setCategroyName(HttpSession session,Integer categroyid,String categroyname){

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请登录！");
        }
        //校验一下是否管理员
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //更新categroyname
            return iCategroyService.setCategroyName(categroyname,categroyid);
        } else {
            return ServiceResponse.createByError("无权限在操作，需要管理员权限！");
        }
    }


}
